/*
* Copyright 2010 Competentum group
*
* Licensed under the GNU General Public License, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.gnu.org/licenses/gpl.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.competentum.engines
{
	import com.competentum.controls.panel.CommandPanel;
	import com.competentum.events.EWTParserCommandEvent;
	import com.competentum.events.EventReasons;
	import com.competentum.events.WorkloadEvent;
	import com.competentum.io.IEngineView;
	import com.competentum.lib.events.CommandEvent;
	import com.competentum.lib.events.ParserEngineCommandEvent;
	import com.competentum.popups.AboutPopup;
	import com.competentum.popups.PreferencesPopup;
	import com.competentum.vos.AbstractItemVO;
	import com.competentum.windows.ScopesManager;
	
	import flash.events.MouseEvent;
	
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;

	/**
	 * Sends commands by top buttons panel
	 * 
	 * @aithor Valentine Abanosimov
	 * */
	public class CommandPanelEngine extends EngineWithView
	{
		public static const TYPE:String = "CommandPanelEngine";
		
		override public function addView(view:IEngineView):void
		{
			super.addView(view);
			view.addEventListener(FlexEvent.CREATION_COMPLETE, viewComplete);
		}
		
		public function CommandPanelEngine()
		{
			super(TYPE);
		}
		
		private function viewComplete(event:FlexEvent):void
		{
			var panel:CommandPanel = event.currentTarget as CommandPanel;
			panel.addNewEmployee.addEventListener(MouseEvent.CLICK, onAddNewEmployeeButtonClick);
			panel.addNewProjectButton.addEventListener(MouseEvent.CLICK, onAddNewProjectClick);
			panel.aboutButton.addEventListener(MouseEvent.CLICK, onAboutButtonClick);
			panel.exportButton.addEventListener(MouseEvent.CLICK, onExport);
			panel.importButton.addEventListener(MouseEvent.CLICK, onImport);
		}
		
		
		private function onAboutButtonClick(event:MouseEvent):void
		{
			PopUpManager.centerPopUp(PopUpManager.createPopUp(ScopesManager.currentWindow, AboutPopup, true));
		}
		
		private function onAddNewEmployeeButtonClick(event:MouseEvent):void
		{
			var wEvent:WorkloadEvent = new WorkloadEvent(CommandEvent.ACTIVATE_ENGINE);
			wEvent.commandType = EmployeesEngine.TYPE;
			wEvent.reason = EventReasons.ADD;
			var item:AbstractItemVO = new AbstractItemVO();
			item.hours = PreferencesPopup.employeeDefaultHours;
			item.name = 'team';
			wEvent.valueItem = item;
			dispatchEvent(wEvent);
			wEvent = new WorkloadEvent(CommandEvent.ACTIVATE_ENGINE);
			wEvent.commandType = EmployeesEngine.TYPE;
			wEvent.reason = EventReasons.REFRESH;
			dispatchEvent(wEvent);
		}
		
		private function onAddNewProjectClick(event:MouseEvent):void
		{
			var wEvent:WorkloadEvent = new WorkloadEvent(CommandEvent.ACTIVATE_ENGINE);
			wEvent.commandType = ProjectsEngine.TYPE;
			wEvent.reason = EventReasons.ADD;
			var item:AbstractItemVO = new AbstractItemVO();
			item.hours = PreferencesPopup.projectDefaultHours;
			item.name = 'group';
			wEvent.valueItem = item;
			dispatchEvent(wEvent);
			wEvent = new WorkloadEvent(CommandEvent.ACTIVATE_ENGINE);
			wEvent.commandType = ProjectsEngine.TYPE;
			wEvent.reason = EventReasons.REFRESH;
			dispatchEvent(wEvent);
		}
		
		private function onExport(event:MouseEvent = null):void
		{
			var pEvent:EWTParserCommandEvent = new EWTParserCommandEvent(CommandEvent.ACTIVATE_ENGINE);
			pEvent.commandType = CSVEngine.TYPE;
			pEvent.reason = ParserEngineCommandEvent.START_SAVING_AS_CONFIGURATION;
			dispatchEvent(pEvent);
		}
		
		private function onImport(event:MouseEvent = null):void
		{
			var pEvent:EWTParserCommandEvent = new EWTParserCommandEvent(CommandEvent.ACTIVATE_ENGINE);
			pEvent.commandType = CSVEngine.TYPE;
			pEvent.reason = ParserEngineCommandEvent.START_READING;
			dispatchEvent(pEvent);
		}
	}
}